﻿@*
    Copyright 2014 Francois Karman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*@
<div class="row">
    <div class="col-lg-4 col-md-6">
        <ul class="nav nav-tabs" style="margin-top: 20px">
            <li class="nav-header">
                Schedulers state
            </li>
            <li class="nav-right">
                <a href="#statesList" data-toggle="tab">
                    <span class="glyphicon glyphicon-align-justify"></span>
                </a>
            </li>
            <li class="nav-right active">
                <a href="#statesChart" data-toggle="tab">
                    <span class="glyphicon glyphicon-adjust"></span>
                </a>
            </li>
        </ul>
        <div class="tab-content">
            <div id="statesChart" class="loading tab-pane fade in active" style="height: 300px;"></div>
            <div id="statesList" class="tab-pane fade">
                <table class="table" data-toggle="table" data-url="api/schedulers" data-cache="false" data-sort-name="SchedulerInstanceId">
                    <thead>
                        <tr>
                            <th data-field="SchedulerInstanceId" data-sortable="true">Instance</th>
                            <th data-field="State" data-sortable="true">State</th>
                            <th data-field="ChangeDate" data-sortable="true" data-formatter="dateTimeFormatter">Since</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-8 col-md-6">
        <h2>Last task errors</h2>
        <div id="taskErrors">
            <table class="table" data-toggle="table" data-url="api/schedulers/errors" data-cache="false" data-sort-name="FireDate">
                <thead>
                    <tr>
                        <th data-field="FireDate" data-sortable="true" data-formatter="dateTimeFormatter">Date</th>
                        <th data-field="Level" data-sortable="true">Level</th>
                        <th data-field="Message" data-sortable="true">Message</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
<h2>Existing jobs</h2>
<table class="table" data-toggle="table" data-url="api/jobs" data-cache="false" data-sort-name="Key">
    <thead>
        <tr>
            <th data-field="Key" data-sortable="true" data-formatter="linkKeyFormatter">Key</th>
            <th data-field="JobDefinition" data-sortable="true">Job type</th>
            <th data-field="NextFireTime" data-sortable="true" data-formatter="dateTimeFormatter">Next activation</th>
        </tr>
    </thead>
</table>
@section scripts
{
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] });
        google.setOnLoadCallback(drawChart);

        // retrieve and display the count of schedulers by state
        function drawChart() {
            $.getJSON('api/schedulers/by-states', function (data) {
                var table = [];
                table[0] = ['State', 'Count'];
                var index = 1;
                $.each(data, function (k, v) {
                    table[index] = [k, v];
                    index++;
                });
                var dataTable = google.visualization.arrayToDataTable(table);
                var options = {
                    legend: { position: 'top', textStyle: { fontSize: 14 } },
                };

                var chart = new google.visualization.PieChart(document.getElementById('statesChart'));
                chart.draw(dataTable, options);
            });
        }

        function dateTimeFormatter(value) {
            return value == "" || value == undefined ? "<i>Never</i>" : moment(value).format('lll');
        }

        function linkKeyFormatter(value, item) {
            return '<a href="' + item.GroupLink + '">' + item.Group + '</a>.<a href="' + item.DetailLink + '">' + item.Name + '</a>';
        }
    </script>
}